<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{obj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="obj.aut_name" :label="formatime(obj.pubdate)">
        <template #icon>
          <img :src="obj.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button
            v-if="obj.is_followed"
             type="info"
              size="mini"
            @click="followedFn(true)">已关注</van-button>
            <van-button v-else
            icon="plus"
             type="info"
              size="mini" plain
            @click="followedFn(false)">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="obj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button @click="attitudeFn" v-if="obj.attitude > 0" icon="good-job" type="danger" size="small">已点赞</van-button>
        <van-button @click="attitudeFn" v-else icon="good-job-o" type="danger" plain size="small">点赞</van-button>
      </div>
      <componentList :arr="componentList"
      @like="likeFn"
      :more="moreFn"
      ref="com"
      :allComomentCount= allcount>
      </componentList>
    </div>
  </div>
</template>

<script>
import { articleDetailAPI, followedAPI, unfollowedAPI, delLikeAPI, addLikeAPI, componentListAPI, comomendLikingAPI, comomendUnLikingAPI } from '@/api'
import { formatime } from '@/utils/date.js'
import componentList from './component/componentList.vue'
export default {
  components: {
    componentList

  },
  data () {
    return {
      obj: {},
      componentList: [],
      offset: '',
      allcount: 0
    }
  },
  async created () {
    const [, res] = await articleDetailAPI({
      artId: this.$route.query.art_id
    })
    console.log(res)
    this.obj = res.data.data
    this.getComomentList()
  },
  methods: {
    formatime: formatime,
    async getComomentList () {
      const [, res1] = await componentListAPI({
        artId: this.$route.query.art_id,
        offset: this.offset
      })
      if (res1.data.data.results.length === 0) {
        this.$refs.com.finished = true
      } else {
        this.offset = res1.data.data.last_idss
      }
      this.allcount = res1.data.data.total_count
      console.log(res1)
      this.componentList = [...this.componentList, ...res1.data.data.results]
      this.$refs.com.loading = false
    },
    async followedFn (bool) {
      if (bool === true) {
        // 点已关注
        const [err] = await unfollowedAPI({
          autId: this.obj.aut_id
        })
        if (!err) this.obj.is_followed = false
      } else {
        // 点关注
        const [err] = await followedAPI({
          autId: this.obj.aut_id
        })
        if (!err) this.obj.is_followed = true
      }
    },
    async attitudeFn (bool) {
      if (bool === true) {
        const [err] = await delLikeAPI({
          autId: this.obj.art_id
        })
        if (!err) this.obj.attitude = -1
      } else {
        const [err] = await addLikeAPI({
          artId: this.obj.art_id
        })
        if (!err) this.obj.attitude = 1
      }
    },
    async likeFn (bool, obj) {
      if (bool === true) {
        obj.is_liking = false
        const [err] = await comomendUnLikingAPI({
          commendId: obj.com_id
        })
        if (!err) obj.is_liking = false
      } else {
        const [err] = await comomendLikingAPI({
          commendId: obj.com_id
        })
        if (!err) obj.is_liking = true
      }
    },
    moreFn () {
      this.getComomentList()
    }

  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
